package cn.ygh.bbo.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.ygh.bbo.framerwork.core.Grid;
import cn.ygh.bbo.common.exception.TipException;
import cn.ygh.bbo.common.annotation.DataScope;
import cn.ygh.bbo.system.bean.RolePermission;
import cn.ygh.bbo.system.mapper.RolePermissionMapper;
import cn.ygh.bbo.system.service.RolePermissionService;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述：角色权限  ServiceImpl
 * @author ygh
 */
@Service
public class RolePermissionServiceImpl implements RolePermissionService{

    private final Logger logger=LoggerFactory.getLogger(getClass());

    @Autowired
    RolePermissionMapper mapper;

    /**
    * 保存对象
    * @param  rolePermission 对象信息
    * @return
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(RolePermission rolePermission) {
        //保存之前通用处理
        beforeCreate(rolePermission);
        rolePermission.def();
        return mapper.insert(rolePermission);
    }

    /**
    * 更新对象
    * @param  rolePermission 对象信息
    * @return
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(RolePermission rolePermission) {
        if(rolePermission.getId()==null){
            throw new TipException("id can not null");
        }
        //更新之前通用处理
        beforeUpdate(rolePermission);
        rolePermission.def();
        return mapper.updateById(rolePermission);
    }

    /**
    * 批量更新
    * @param  rolePermission 对象信息
    * @param ids
    * @return
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateBatchByIds(RolePermission rolePermission, Serializable[] ids) {
        if(ids==null || ids.length==0){
            throw new TipException("ids can not null");
        }
        return mapper.updateBatchByIds(rolePermission,ids);
    }

    /**
    * 根据ID删除对象
    * @param id
    * @return
    */
    @Override
    public int deleteById(Serializable id) {
        if(id==null){
            throw new TipException("id can not null");
        }
        if(checkLink(id)){
            return mapper.deleteById(id);
        }
        return 0;
    }

    /**
    * 删除关联判断
    * @param id
    */
    private boolean checkLink(Serializable id) {
        return true;
    }

    /**
    * 根据ID数组批量删除对象
    * @param ids
    * @return
    */
    @Override
    public int deleteByIds(Serializable[] ids) {
        if(ids==null || ids.length==0){
            throw new TipException("ids can not null");
        }
        int result=0;
        for (Serializable id:ids){
            result+=deleteById(id);
        }
        return result;
    }

    /**
    * 根据ID获取对象
    * @param id
    * @return
    */
    @Override
    public RolePermission getById(Serializable id) {
        if(id==null){
            throw new TipException("id can not null");
        }
        return mapper.selectById(id);
    }

    /**
    * 根据ID数组获取对象列表
    * @param ids
    * @return
    */
    @Override
    public List<RolePermission> listfindByIn(Serializable[] ids) {
        if(ids==null || ids.length==0){
            throw new TipException("ids can not null");
        }
        return mapper.selectBatchIds(Arrays.asList(ids));
    }

    /**
    * 根据对象信息获取满足条件的对象列表
    * @param  rolePermission 对象信息
    * @param pageable 分页条件为空则不分页
    * @return
    */
    @DataScope
    @Override
    public List<RolePermission> listfind(RolePermission rolePermission, Pageable pageable) {
        if (pageable!=null && pageable.getPageNumber()!=0){
            PageHelper.startPage(pageable.getPageNumber()
                    , pageable.getPageSize(), false);
        }
        return mapper.selectByObj(rolePermission);
    }

    /**
    * 根据对象信息获取满足条件的对象列表和总数
    * @param  rolePermission 对象信息
    * @param pageable 分页条件
    * @return
    */
    @DataScope
    @Override
    public Grid listGrid(RolePermission rolePermission, Pageable pageable) {
        PageHelper.startPage(pageable.getPageNumber()
                , pageable.getPageSize(), true);
        List<RolePermission> rolePermissions = mapper.selectByObj(rolePermission);
        PageInfo<RolePermission>pageInfo=new PageInfo<>(rolePermissions);
        Grid grid=new Grid();
        grid.setTotal((int) pageInfo.getTotal());
        grid.setRows(pageInfo.getList());
        return grid;
    }

    /**
     * 获取角色的相关权限关联
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<RolePermission> listfindByRoleIds(List<Integer> roleIds) {
        if (roleIds.size()==0){
            return new ArrayList<>();
        }
        RolePermission params=new RolePermission();
        if (roleIds.size()>1){
            params.setRoleIds(roleIds);
        }else {
            params.setRoleId(roleIds.get(0));
        }
        return listfind(params, null);
    }

    /**
     * 获取相关数量
     *
     * @param roleId
     * @param permissionId
     * @return
     */
    @Override
    public long getCountByRoleIdAndPermissionId(Integer roleId, Integer permissionId) {
        RolePermission params=new RolePermission();
        params.setRoleId(roleId);
        params.setPermissionId(permissionId);
        return mapper.selectCountByObj(params);
    }

    /**
     * 删除角色的所有权限关联
     *
     * @param roleId
     */
    @Override
    public void deleteByRoleId(Integer roleId) {
        mapper.deleteByRoleId(roleId);
    }
}
